/**
 * DisplayView implementation 
**/

#include <unistd.h>
#import "CDList.h"
#import "SetupView.h"
#import "ToggleDisplayView.h"

@implementation ToggleDisplayView : UIView

- (id) initWithFrame: (struct CGRect)rect withApp: (UIApplication*)app withCDList: (CDList*)cdlist withSetupView: (SetupView*)setupView
{
	_application = app;
	_cdlist = cdlist;
	_setupView = setupView;
	
	//Init view with frame rect
	[super initWithFrame: rect];
	
	_table = [[UIPreferencesTable alloc] initWithFrame: CGRectMake(0.0f, 42.0f, rect.size.width, rect.size.height)];
	[_table setDelegate:self];
	[_table setDataSource:self];
	
	// Build Group
	_prefGroup = [[UIPreferencesTableCell alloc] initWithFrame: CGRectMake(0.0f, 42.0f, rect.size.width, 24.0f)];
	[_prefGroup setTitle: @""];
	_spacerGroup = [[UIPreferencesTableCell alloc] initWithFrame: CGRectMake(0.0f, 42.0f, rect.size.width, 24.0f)];
	[_spacerGroup setTitle: @""];
	
	[self addSubview: _table];	
	[self buildPreferencesTable];
	
	return self;
	
}

// return to the preference table view
- (void) makeSetupActive
{
	[_setupView makeSetupActive];
}

/**
 * This builds of up the section list table, headers and all, with test data
**/
- (void) buildPreferencesTable
{
	// setup nav bar
	[_navbar release];
	_navbar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 42.0f)];
	UINavigationItem* title = [[UINavigationItem alloc] initWithTitle:@"Display Type"];
	[_navbar pushNavigationItem:title];
	[_navbar setBarStyle: 0];
	[_navbar enableAnimation];
	
	//Setup Save button
	UINavBarButton* _backButton = [[UINavBarButton alloc] initWithFrame: CGRectMake(3.0f, 5.0f, 65.0f, 29.0f)];
	[_backButton setAutosizesToFit: FALSE];
	[_backButton setTitle: @"  BACK"];
	[_backButton addTarget: self action: @selector(makeSetupActive) forEvents: 1];
	[_backButton setNavBarButtonStyle: 2];
	[_navbar addSubview: _backButton];
	
	//setup the new preferences to display
	[_tableCells release];
	_tableCells = [[NSMutableArray alloc] init];
	
	UIPreferencesTableCell* cell = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 42.0f)];
	[cell setTitle:@"Days:H:M:S"];
	[cell setCheckStyle:0];
	if ([_cdlist getSelectedCounterDisplayType] == 0) [cell setChecked:TRUE];
	[_tableCells addObject: cell];
	
	cell = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 42.0f)];
	[cell setTitle:@"Seconds Only"];
	[cell setCheckStyle:0];
	if ([_cdlist getSelectedCounterDisplayType] == 1) [cell setChecked:TRUE];
	[_tableCells addObject: cell];
	
	cell = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 42.0f)];
	[cell setTitle:@"Moons:D:H:M:S"];
	[cell setCheckStyle:0];
	if ([_cdlist getSelectedCounterDisplayType] == 2) [cell setChecked:TRUE];
	[_tableCells addObject: cell];
		
	//Refresh the fileview table
	[self addSubview: _navbar];
	[_table reloadData];
}

/**
 * Delegate Methods
**/
- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable*)aTable
{
	return 3;
}

- (int)preferencesTable:(UIPreferencesTable*)aTable numberOfRowsInGroup:(int)group
{
	switch( group )
	{
		case 0: return 1;
		case 1: return 1;
		case 2: return [_tableCells count];
		default: return 0;
	}
}

- (UIPreferencesTableCell*)preferencesTable:(UIPreferencesTable*)aTable cellForGroup:(int)group
{
	switch ( group )
	{
		case 0: return _spacerGroup;
		case 1: return _prefGroup;
		case 2: return _prefGroup;
		default: return Nil;
	}
}

- (float)preferencesTable:(UIPreferencesTable*)aTable heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposed
{
	float groupLabelSize = 5.0f;
	switch( group )
	{
		case 0: return groupLabelSize;
		case 1: return groupLabelSize;
		default: return proposed;
	}
}

- (BOOL)preferencesTable:(UIPreferencesTable*)aTable isLabelGroup:(int)group
{
	switch ( group )
	{
		case 0: return FALSE;
		case 1: return FALSE;
		default: return FALSE;
	}
}

- (UIPreferencesTableCell*)preferencesTable:(UIPreferencesTable*)aTable cellForRow:(int)row inGroup:(int)group
{
	switch( group )
	{
		case 0: return _spacerGroup;
		case 1: return _prefGroup;
		case 2: return [_tableCells objectAtIndex:row];
	}
}

- (void) tableRowSelected: (NSNotification*) notification 
{
	switch( [_table selectedRow] )
	{
		case 5: [_cdlist setSelectedCounterDisplayType:[[NSNumber alloc] initWithInt:0]]; break;
		case 6: [_cdlist setSelectedCounterDisplayType:[[NSNumber alloc] initWithInt:1]]; break;
		case 7: [_cdlist setSelectedCounterDisplayType:[[NSNumber alloc] initWithInt:2]]; break;
	}
	[self buildPreferencesTable];
}


- (void) setDelegate: (id)delegate;
{
	_delegate = delegate;
	
	[self buildPreferencesTable];
}

@end


	
